package pt.utl.ist.calcNotas;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


public class FantasticAdapter extends ArrayAdapter<Elemento> {

    private LayoutInflater inflater = null;
    private ArrayList<Cadeira> cadeiras;
    private ArrayList<Elemento> elementos;
    private Context context;
    private Activity activity;


    public FantasticAdapter(Activity activity, Context context, ArrayList<Elemento> elementos, ArrayList<Cadeira> cadeiras){
        super(context, R.layout.componente_fantastico, elementos);
        this.context = context;
        this.cadeiras = cadeiras;
        this.elementos = elementos;
        this.activity = activity;
    }



    @Override
    public View getView(final int i, View view, ViewGroup parent) {
        final Elemento e = elementos.get(i);
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View v = inflater.inflate(R.layout.componente_fantastico, parent, false);
        final TextView txtNota = (TextView)v.findViewById(R.id.textViewNota);
        final  TextView campoPeso=(TextView)v.findViewById(R.id.campoPeso);
        int nota = ((e.getNotaMinima()<e.getValor())? e.getValor(): e.getNotaMinima());
        txtNota.setText("Nota: "+(nota/10f));

        TextView txt = (TextView)v.findViewById(R.id.textViewNomeElemento);
        txt.setText(elementos.get(i).getNome());
        ImageButton btn = (ImageButton)v.findViewById(R.id.editarB);
        ImageButton deletebtn = (ImageButton)v.findViewById(R.id.deleteNow);
        campoPeso.setText("Peso:"+e.getPeso()+"%");

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((CadeiraFantastica)activity).editElementoFantastico(i);
            }
        });

        deletebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((CadeiraFantastica)activity).removeElemento(i);
            }

        });

        SeekBar sb = (SeekBar)v.findViewById(R.id.seekBarComponente);
        sb.setMax(200-e.getNotaMinima());
        sb.setProgress(e.getValor()-e.getNotaMinima());
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int v, boolean b) {
                txtNota.setText("Nota: "+(v+e.getNotaMinima())/10f);
                e.setValor(v + e.getNotaMinima());
                ((CadeiraFantastica)activity).calculaNota();
                SaveData();
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        return v;
    }

    protected void SaveData(){
        try
        {
            String path = context.getApplicationContext().getFilesDir() + "/storage/";
            File file = new File(path);
            file.mkdirs();
            path+="cadeiras.sav";
            ObjectOutputStream oos = new ObjectOutputStream(
                    new FileOutputStream(path, false));
            oos.writeObject(cadeiras);
            oos.flush();
            oos.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}
